﻿using Microsoft.EntityFrameworkCore;
using NLog.Web;
using Outpatient.Infrastructure;
using OutpatientApi.Common;
using OutpatientApi.Methodimplementation.IServices;
using OutpatientApi.Methodimplementation.Services;

namespace OutpatientApi.Extensions
{
    /// <summary>
    /// 拓展类方法  静态类  静态方法  this关键字
    /// </summary>
    public static class ServiceCollectionExtensions
    {

        public static WebApplicationBuilder InJect(this WebApplicationBuilder builder)
        {

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            //中文注释
            builder.Services.AddSwaggerGen(x =>
            {
                var file = Path.Combine(AppContext.BaseDirectory + "OutpatientApi.xml");
                x.IncludeXmlComments(file, true);
            });

            //配置EFcore数据库
            builder.Services.AddDbContext<EFDbContext>(x =>
            {
                x.UseMySql(builder.Configuration.GetConnectionString("Conn")
                , ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("Conn")), action =>
                {
                    //配置EFCore的重试策略
                    action.EnableRetryOnFailure(
                        maxRetryCount: 10,//最大重试次数
                        maxRetryDelay: TimeSpan.FromSeconds(60),//每次重试间隔时间最大延迟
                        errorNumbersToAdd: null
                        );

                }).EnableSensitiveDataLogging();//启动敏感数据日志记录，使与调试 但在生产环境中应谨慎使用避免泄露敏感信息

                //配置日志记录
                x.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddFilter(
                    category: DbLoggerCategory.Database.Name,//类别
                    level: LogLevel.Information//控制台日志
                    ).AddConsole()));
            });

            //注入
            builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BasreRepository<>));


            //登录配置
            builder.Services.AddScoped<IUserService, UserService>();
            // 注册咨询分类服务
            builder.Services.AddScoped<IConsultationCategoryService, ConsultationCategoryService>();
            // 注册病人挂号服务
            builder.Services.AddScoped<IPatientRegistrationService, PatientRegistrationService>();
            // 注册住院管理服务
            builder.Services.AddScoped<IInpatientManagement, InpatientManagement>();
            // 注册药房管理服务
            builder.Services.AddScoped<IPharmacymanagementServices,PharmacymanagementServices>();
            //AutoMapper
            builder.Services.AddAutoMapper(typeof(MapperingProfil));
            //区分大小写
            builder.Services.AddControllersWithViews().AddJsonOptions(p => p.JsonSerializerOptions.PropertyNamingPolicy = null);
            //NLog注入
            builder.Host.UseNLog();



            return builder;
        }
    }
}
